<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2017/1/23
 * Time: 上午12:06
 */

namespace Conf;


use Core\AbstractInterface\AbstractEvent;
use Core\AutoLoader;
use Core\Component\Logger;
use Core\Http\Request;
use Core\Http\Response;
use App\Utility\Mysql;
use App\Game\Task;
use App\Game\Worker;

class Event extends AbstractEvent
{
    public $redis;
    public $db;
    function frameInitialize()
    {
        // TODO: Implement frameInitialize() method.
        date_default_timezone_set('Asia/Shanghai');
        $loader = AutoLoader::getInstance();
        $loader->requireFile("App/Vendor/MysqliDb/MysqliDb.php");
    }

    function beforeWorkerStart(\swoole_http_server $server)
    {
        // TODO: Implement beforeWorkerStart() method.

        //实例化 redis
        $this->redis = new \Redis();
        $link = $this->redis->connect('127.0.0.1', 6379);
        if($link){
            Logger::console("Connection to server sucessfully <br/>");
            $this->redis->select(8);
            $this->redis->FLUSHDB();
        }else{
            throw new \Exception("redis cant connect");
        }
        //实例化mysql
        $this->db = (new Mysql())->getDb();
        $listener = $server->addlistener("0.0.0.0",9502,SWOOLE_TCP);
        //混合监听tcp时    要重新设置包解析规则  才不会被HTTP覆盖，且端口不能与HTTP SERVER一致 HTTP本身就是TCP
        $listener->set(array(
            "open_eof_check"=>false,
            "package_max_length"=>2048,
            'heartbeat_check_interval' => 5,
            'heartbeat_idle_time' => 60,
        ));
        $worker = new Worker($this->redis);
        $listener->on("connect",function(\swoole_server $server,$fd,$swoole_client) use($worker){
            $worker->connect($server,$fd,$swoole_client);
        });
        $listener->on("receive",function(\swoole_server $server,$fd,$from_id,$data) use($worker){
            $worker->receive($server,$fd,$from_id,$data);
        });
        $listener->on("close",function (\swoole_server $server,$fd) use($worker){
            $worker->close($server,$fd);
        });
    }

    function onStart(\swoole_http_server $server)
    {
        // TODO: Implement onStart() method.
    }

    function onShutdown(\swoole_http_server $server)
    {
        // TODO: Implement onShutdown() method.
    }

    function onWorkerStart(\swoole_server $server, $workerId)
    {
        // TODO: Implement onWorkerStart() method.
    }

    function onWorkerStop(\swoole_server $server, $workerId)
    {
        // TODO: Implement onWorkerStop() method.
    }

    function onRequest(Request $request, Response $response)
    {
        // TODO: Implement onRequest() method.
    }

    function onDispatcher(Request $request, Response $response, $targetControllerClass, $targetAction)
    {
        // TODO: Implement onDispatcher() method.
    }

    function onResponse(Request $request,Response $response)
    {
        // TODO: Implement afterResponse() method.
    }

    function onTask(\swoole_http_server $server, $taskId, $fromId, $taskObj)
    {
        // TODO: Implement onTask() method.
        $taskObj = json_decode($taskObj);
        $task = new Task($server,$taskObj,$this->redis,$this->db);
        $task->{$taskObj->action}();
    }

    function onFinish(\swoole_http_server $server, $taskId, $fromId, $taskObj)
    {
        // TODO: Implement onFinish() method.
        $taskObj = json_decode($taskObj);
        switch($taskObj->action){
            case 'auth_fail':
                $server->close($taskObj->fd);
                break;
        }
    }

    function onWorkerError(\swoole_http_server $server, $worker_id, $worker_pid, $exit_code)
    {
        // TODO: Implement onWorkerError() method.
    }
}
